package com.winter.admin.controller;

import com.alibaba.fastjson.JSON;
import com.winter.common.constant.BaseMessage;
import com.winter.common.dto.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试接口 Controller
 *
 * @author zhangshuaiyin
 * @date 2021-06-01 14:48
 */
@Api(value = "测试接口-这里无用", tags = "测试接口文档-侧边栏分类")
@RestController
public class HelloController {

    /**
     * ApiImplicitParam: 指定参数
     * ApiOperation: 接口描述
     *
     * @return hello world
     */
    @ApiOperation(value = "这是hello下面的", notes = "notes 为接口描述，value 为侧边栏标题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map", value = "参数的简要说明", required = true, dataTypeClass = Map.class)
    })
    @PostMapping("/hello/world")
    public BaseResponse<String> world(@ApiParam(name = "map", value = "参数的简要说明", required = true) @RequestBody Map<String, Object> map) {
        return BaseResponse.ok("Hello World. " + JSON.toJSONString(map));
    }

    @ApiOperation(value = "用户登录", notes = "登录接口测试")
    @PostMapping("/user/login")
    public BaseResponse<Map<String, String>> login(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        if (!"admin".equals(username)) {
            throw BaseMessage.USER_NAME_ERROR.exception();
        }
        if (!"123456".equals(password)) {
            throw BaseMessage.PASSWORD_ERROR.exception();
        }
        Map<String, String> token = new HashMap<>(1);
        token.put("token", "admin-token");
        return BaseResponse.ok(token);
    }

    @ApiOperation(value = "获取用户基础信息", notes = "登录接口测试")
    @GetMapping("/user/info")
    public BaseResponse<Map<String, String>> getUserInfo(String token) {
        Map<String, String> info = new HashMap<>(4);
        info.put("roles", "['admin']");
        info.put("introduction", "I am a super administrator");
        info.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        info.put("name", "测试管理员");
        info.put("token", token);
        return BaseResponse.ok(info);
    }

    @ApiOperation(value = "退出登录", notes = "退出登录测试")
    @PostMapping("/user/logout")
    public BaseResponse<BaseMessage> logout() {
        return BaseResponse.ok();
    }
}
